﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using ArkanoidX.UI;
using ArkanoidX.Objects;

namespace ArkanoidX
{
    public class Level
    {
        /**
         * level profile
         */

        public int LevelNumber;
        public String Password;

        public MovingText LevelText;

        /**
         * level state
         */
        public enum State
        {
            Created,
            Running,
            Stopped,
        }

        public State LevelState = State.Created;

        /**
         * game state
         */
        public int Score = 0;


        /**
         * level properties
         */
        public int NumOfRows = -1;

        /**
         * owner game
         */

        public MainGame Game;

        /**
         * <code>
         * <summary>
         *  game objects
         * </summary>
         */
        public Bricks Bricks;
        public Racket Racket;
        public Ball Ball;
        public Galaxy galaxy;

        public int Lives;

        /**
         * </code>
         */

        /**
         * time
         */
        public TimeSpan _initialTime = default(TimeSpan);
        public TimeSpan _levelTitleDuration = new TimeSpan(0, 0, 1);


        MessageDialog quitConfirm = null;

        public bool Stopped
        {
            get { return LevelState == State.Stopped;  }
        }

        /**
         * bounds of game region
         */
        public Rectangle ArenaRegion;

        public Level(MainGame game)
            : this(game, 0, null, null, null)
        {
        }


        public Level(MainGame game, int levelNumber, Bricks bricks, Racket racket, Ball ball)
        {
            this.Game = game;
            this.LevelNumber = levelNumber;

            this.Bricks = bricks;
            this.Racket = racket;
            this.Ball = ball;

            if (this.Bricks != null)
                this.Bricks.Level = this;

            if (this.Racket != null)
                this.Racket.Level = this;

            if (this.Ball != null)
                this.Ball.Level = this;

            ArenaRegion = game.Arena;

            LevelText = new MovingText(game);
            LevelText.Value = String.Format("Level {0}", levelNumber);
            LevelText.Color = Color.Red;
            LevelText.Align(this.ArenaRegion, Window.AlignType.Center, 0, 0);

            galaxy = new Galaxy(game);
        }

        public virtual void Start()
        {
            Game.SoundResources.PlayLevelIntro();

            /*
             * start the level with intializing level parameters, 
             * setting up running envirionment and getting it to run 
             */

            // clear all components
            Game.ClearAllComponents();

            if (Racket != null)
                Racket.AddToLayer(Game.RacketLayer);
            if (Bricks != null)
                Bricks.AddToLayer(Game.BricksLayer);
            if (Ball != null)
                Ball.AddToLayer(Game.BallLayer);

            if (galaxy != null)
                galaxy.AddToLayer(Game.BaseLayer);

            new RightPanel(Game, Game.BaseLayer, this).Show();

            LevelText.Show();

            LevelState = State.Running;
        }

        public virtual void Finish()
        {
            LevelState = State.Stopped;
        }

        public virtual void Update(GameTime gameTime)
        {
            if (_initialTime == default(TimeSpan))
            {
                _initialTime = gameTime.TotalGameTime;
            }
            if (LevelText != null &&
                gameTime.TotalGameTime - _initialTime >= _levelTitleDuration)
            {
                LevelText.Close();
                LevelText = null;
            }

            KeyboardState kbs = Keyboard.GetState();
            if (kbs.IsKeyDown(Keys.Escape) && !Game.lkbsGame.LastEscPressed && quitConfirm == null)
            {
                quitConfirm = new MessageDialog(Game, null, "Are you sure\nyou want to quit?\nEnter - Y, Esc - N");
                quitConfirm.Show();
                Pause();
            }

            if (quitConfirm != null && quitConfirm.state == Window.State.Closed)
            {
                if (quitConfirm.GetModalResult() == Window.ModalResult.OK)
                    Quit();
                else
                    Resume();
                quitConfirm = null;
            }
        }

        public virtual void Quit()
        {
            Game.Restart();
        }

        public virtual void Pause()
        {
            Game.Pause();
        }

        public virtual void Resume()
        {
            Game.Resume();
        }
    }
}
